import 'package:get/get.dart';

import '../../../models/category_model.dart';
import '../../../services/httpsClient.dart';

class CategoryController extends GetxController {
  final count = 0.obs;
  RxInt selectIndex = 0.obs;

  RxList<CategoryItemModel> leftCategoryList = <CategoryItemModel>[].obs;
  RxList<CategoryItemModel> rightCategoryList = <CategoryItemModel>[].obs;
  HttpsClient client = HttpsClient();
  @override
  void onInit() {
    super.onInit();
    getLeftCategoryData();
  }

  void increment() => count.value++;

  void changeIndex(index, id) {
    selectIndex.value = index;
    getRightCategoryData(id);
    update();
  }

  getLeftCategoryData() async {
    //var response = await Dio().get("https://miapp.itying.com/api/pcate");
    var response = await client.get('api/pcate');
    if (response != null) {
      var category = CategoryModel.fromJson(response.data);
      leftCategoryList.value = category.result!;
      getRightCategoryData(leftCategoryList[0].sId!);
      update();
    }
  }

  getRightCategoryData(String pid) async {
    var response = await client.get('api/pcate?pid=$pid');
    if (response != null) {
      var category = CategoryModel.fromJson(response.data);
      print('getRightCategoryData category:$category');
      rightCategoryList.value = category.result!;
      update();
    }
  }
}
